package com.github.lee.unsplash.ui.home

import android.os.Bundle
import android.view.View
import androidx.appcompat.widget.Toolbar
import androidx.core.graphics.drawable.toBitmap
import androidx.fragment.app.viewModels
import androidx.palette.graphics.Palette
import coil.load
import com.github.lee.core.common.view.RatioImageView
import com.github.lee.mvvm.core.databinding.LayoutDefaultLoadingBinding
import com.github.lee.mvvm.core.ui.BaseRefreshFragment
import com.github.lee.mvvm.core.util.viewGone
import com.github.lee.unsplash.R
import com.github.lee.unsplash.databinding.ToolbarHomeBinding
import com.github.lee.unsplash.manager.LoginManager
import com.github.lee.unsplash.utils.dialog.CenterTipsDialog
import dagger.hilt.android.AndroidEntryPoint
import xyz.belvi.blurhash.BlurHash
import javax.inject.Inject

@AndroidEntryPoint
class HomeFragment : BaseRefreshFragment<HomeVM>() {

    @Inject
    lateinit var lm: LoginManager

    private val blurHash by lazy {
        BlurHash(mContext, 20, 1.0F)
    }
    private val headerImageView: RatioImageView by lazy {
        RatioImageView(mContext)
    }
    private val toolbarVB by lazy {
        ToolbarHomeBinding.inflate(layoutInflater)
            .also {
                it.tlHome.title = ""
            }
    }

    companion object {
        fun newInstance(): HomeFragment {
            return HomeFragment()
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        blurHash.clean()
    }

    override fun onCreateViewModel(): HomeVM = viewModels<HomeVM>().value

    override fun onCreateToolbar(): Toolbar =
        toolbarVB.tlHome

    override fun onCreateLoading(): View =
        LayoutDefaultLoadingBinding.inflate(layoutInflater).root

    //===Desc:=========================================================

    override fun onInitData(savedInstanceState: Bundle?) {
        super.onInitData(savedInstanceState)
        isToolbarHover = true
        autoRefresh = false
    }

    override fun onObserveData() {
        super.onObserveData()
        pageVM.apply {
            topPhotoLiveData.observe(viewLifecycleOwner) {
                if (null == it) {
                    viewGone(headerImageView, true)
                    return@observe
                }
                headerImageView.ration = it.width * 1.0F / it.height
                blurHash.execute(it.blurHash, 100, (100 / headerImageView.ration).toInt()) { bd ->
                    val builder = Palette.from(bd.toBitmap())
                    builder.generate { palette -> palette?.darkVibrantSwatch }

                    headerImageView.load(it.urls.raw) {
                        crossfade(true)
                        crossfade(500)
                        placeholder(bd)
                    }
                }
            }
        }

    }

    override fun onSetViewListener() {
        super.onSetViewListener()
        mAdapter.setOnItemChildClickListener { _, view, position ->
            when (view.id) {
                R.id.iv_favorites -> {
                    if (!lm.isLogin) {
                        CenterTipsDialog.showLoginTipDialog(mContext)
                        return@setOnItemChildClickListener
                    }
                    pageVM.likeOrUnlikePhoto(position)
                }
            }
        }

    }

    override fun onSetViewData() {
        super.onSetViewData()
        pageVB.apply {

        }
        getRecyclerView().itemAnimator?.changeDuration = 0
        addHeader(headerImageView)

        pageVM.homeIndex()
    }

    override fun onRefreshOrLoadMore(isRefresh: Boolean) {
        super.onRefreshOrLoadMore(isRefresh)
        pageVM.refreshOrLoad(isRefresh)
    }

    //===Desc:=========================================================


}